package com.action.boot.mongo.mongo_template.dao;

import com.action.boot.mongo.mongo_template.entity.Order;
import com.mongodb.DBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Wuxiang on 2017/9/2.
 */
@Component
public class OrderDao {
    @Autowired
    MongoTemplate mongoTemplate;

    public void addOrder(Order order) {
        mongoTemplate.save(order);
    }

    public List<Order> seleceAll() {
        return mongoTemplate.findAll(Order.class);
    }

    public void json(DBObject dbObject) {
        mongoTemplate.save(dbObject, "json");
    }

    public List<DBObject> find() {
        Query query = new Query();
        query.addCriteria(Criteria.where("1").is("aa"));
        List<DBObject> dbObjects = mongoTemplate.find(query, DBObject.class, "json");
        return dbObjects;
    }

    public void saveOne(DBObject dbObject) {
        mongoTemplate.save(dbObject, "json");
    }

    public void saveList(List<DBObject> dbObjects) {
        mongoTemplate.insert(dbObjects, "json");
    }

}
